# -*- coding: utf-8 -*-
"""
Ensuring message delivery with the mandatory flag
https://pika.readthedocs.io/en/latest/examples/blocking_publish_mandatory.html
"""

import pika
import pika.exceptions

credent = pika.PlainCredentials('guest', 'guest')

connection = pika.BlockingConnection(pika.ConnectionParameters(
        '172.22.23.252',
        credentials=credent
    ))

channel = connection.channel()

channel.queue_declare(queue="test", durable=True, exclusive=False, auto_delete=False)

channel.confirm_delivery()

try:
    # channel.queue_unbind('test', exchange='')
    channel.basic_publish(exchange='',
                          routing_key='test',
                          body='Hello World!',
                          properties=pika.BasicProperties(content_type='text/plain',
                                                          delivery_mode=2),
                          mandatory=True)
    print('Message was published')
except pika.exceptions as e:
    print('Message was returned')

